package me.mononin.dilechat.fragments.account;


import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import net.qiujuer.genius.ui.widget.Loading;

import butterknife.BindView;
import butterknife.OnClick;
import me.mononin.common.common.app.Fragment;
import me.mononin.common.common.app.PresenterFragment;
import me.mononin.dilechat.R;
import me.mononin.dilechat.activities.MainActivity;
import me.mononin.factory.preseneter.account.LoginContract;
import me.mononin.factory.preseneter.account.LoginPresenter;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends PresenterFragment<LoginContract.presenter>
        implements LoginContract.View  {

    @BindView(R.id.edit_phone)
    EditText mPhone;
    @BindView(R.id.edit_password)
    EditText mPassword;


    @BindView(R.id.loading)
    Loading mLoading;

    @BindView(R.id.btn_submit)
    Button mSubmit;

    private AccountTrigger mTrigger;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mTrigger = (AccountTrigger) context;
    }

    @Override
    protected LoginContract.presenter initPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected int getContentId() {
        return R.layout.fragment_login;
    }

    @OnClick({R.id.txt_go_register,R.id.btn_submit})
    public void clickEvent(View view){
        switch (view.getId()){
            case R.id.txt_go_register:
                mTrigger.triggerView();
                break;
            case R.id.btn_submit:
                String phone = mPhone.getText().toString();
                String password = mPassword.getText().toString();
                // 调用P层进行注册
                mPresenter.login(phone,password);
                break;
            default:
                break;
        }
    }

    @Override
    public void loginSuccess() {
        MainActivity.show(getContext());
        getActivity().finish();
    }

    @Override
    public void showError(int str) {
        super.showError(str);
        // 当需要显示错误的时候触发，一定是结束了

        // 停止Loading
        mLoading.stop();
        // 让控件可以输入
        mPhone.setEnabled(true);
        mPassword.setEnabled(true);
        // 提交按钮可以继续点击
        mSubmit.setEnabled(true);
    }

    @Override
    public void showLoading() {
        super.showLoading();

        mLoading.start();
        // 让控件不可以输入
        mPhone.setEnabled(false);
        mPassword.setEnabled(false);
        // 提交按钮不可以继续点击
        mSubmit.setEnabled(false);
    }
}
